package com.lingshu.app.ui.message;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.lingshu.app.BR;
import com.lingshu.app.R;
import com.lingshu.app.database.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 于定金
 * @version 1.0
 * @Date 2024/1/15 14:43
 * @Description
 */
public class MessageFragmentAdapter extends RecyclerView.Adapter<MessageFragmentAdapter.MessageFragmentViewHolder> {
    private List<MessageBean> dataList;
    private Context context;

    public MessageFragmentAdapter(Context context, List<MessageBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public MessageFragmentAdapter.MessageFragmentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_message, parent, false);
        return new MessageFragmentViewHolder(dataBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull MessageFragmentAdapter.MessageFragmentViewHolder holder, int position) {
        holder.binding.setVariable(BR.messageItem, dataList.get(position));
        addViewClickListener(holder.binding.getRoot(), dataList.get(position), position);
        holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    public void setDataList(List<MessageBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public void clearDataList() {
        if (dataList == null) dataList = new ArrayList<>();
        dataList.clear();
    }

    public void addViewClickListener(View root, MessageBean itemData, int position) {

    }

    class MessageFragmentViewHolder extends RecyclerView.ViewHolder {
        ViewDataBinding binding;

        public MessageFragmentViewHolder(@NonNull ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
